summaryrefslogtreecommitdiff
blob: 3a7b3dc1a09262b69a1e474a2646bd8515e51365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/python
import getopt, sys, os, string
from ebuild import *
import phases
from cran_read import *
from filetricks import *
from settings import *

__doc__="Usage: "+sys.argv[0]+" <local repository directory> <action> [<action arguments>...]"

#sync a local repository's PACKAGES file
def action_sync(repo_location,remote_uri):
    import StringIO, urllib
    if not os.path.isdir(os.path.join(repo_location, REPO_MYDIR)):
        os.mkdir(os.path.join(repo_location,REPO_MYDIR))
    packages_filename=os.path.join(repo_location, REPO_MYDIR, 'PACKAGES')
    packages_rds_filename=os.path.join(repo_location, REPO_MYDIR, 'packages.rds')
    try:
        #we first try to get a serialized full database... this works for CRAN
        urllib.urlretrieve(remote_uri+'/web/packages/packages.rds',packages_rds_filename)
        R_script=os.path.join(os.path.dirname(__file__),'convert_packages_rds.R')
        returncode=os.system('R --quiet --file='+R_script+' --args '+packages_rds_filename+' '+packages_filename)
        if returncode:
            raise RuntimeError('Could not convert packages.rds')
    except:
        urllib.urlretrieve(remote_uri+'/src/contrib/PACKAGES',packages_filename)
    repo_file=open(os.path.join(repo_location,REPO_MYDIR,'remote_uri'),'w')
    repo_file.write(remote_uri)
    repo_file.close()

    import rfc822
    packages_file=open(packages_filename,"r")
    file_parts=EmptyLinesFile(packages_file)
    database_dir=os.path.join(repo_location,REPO_MYDIR,'DESCRIPTION')
    try:
        os.makedirs(database_dir)
    except:
        pass
    #read PACKAGES file
    while not file_parts.eof:
        current_package=''
        while True:
            line=file_parts.readline()
            if len(line.strip()): #nonempty line, add to current_package
                current_package=current_package+line
            else:
                break
        rfc822_file=StringIO.StringIO(current_package)
        cran_package=dict(rfc822.Message(rfc822_file).items()) #read part of PACKAGES file
        if len(cran_package):
            pms_package=pmsify_package_data(cran_package,remote_uri) #we need to know the PMS name
            #now add the package to the database
            filename=os.path.join(database_dir,pms_package.ebuild_vars['pn'])
            description_file=open(filename,'w')
            description_file.write(current_package)
            description_file.close()
    return 0

#list categories in this repository
def list_categories(repo_location):
    print "dev-R"
    return 0

#idem ditto
def list_packages(repo_location):
    packages=read_packages(os.path.join(repo_location,REPO_MYDIR,'PACKAGES'),repo_location)
    for package in packages:
        print 'dev-R/'+package.ebuild_vars['pn'],package.ebuild_vars['pv']
    return 0

#list package details, in PMS's format
def action_package(repo_location,package_name):
    defined_phases=[]
    package=find_package(repo_location,package_name[package_name.find('/')+1:])
    #output data
    for key,value in package.ebuild_vars.iteritems():
        if key=='pn' or key=='pv': #readonly vars, we cannot set these in ebuilds
            pass
        elif isinstance(value,str): #if string
            print key.upper()+'='+value.replace('\n','')
        elif isinstance(value,list) and key=='license':
            if len(value)>1:
                print "LICENSE=|| ( "+' '.join(value)+' )'
            else:
                print "LICENSE="+' '.join(value)
        elif isinstance(value,list): #list, concat items
            print key.upper()+'='+' '.join(value).replace('\n','')
    for pms_func in pms_phases:
        if hasattr(phases,pms_func):
            defined_phases.append(pms_func)
    print 'GCOMMON_PHASES='+' '.join(defined_phases)
    return 0

def action_generate_metadata(repo_location):
    #todo, nothing written yet.
    return 0

def usage():
    print __doc__
    return 0

def main():
    arguments=sys.argv[1:]
    #print options, arguments
    if len(arguments)<2: #we need at least a local repository location and an action
        usage()
        sys.exit(0)
    action=arguments[1]
    repo_location=os.path.abspath(arguments[0])
    if action=='sync':
        if len(arguments)<3:
            print "The 'sync' action takes the following parameters:"
            print " * remote_repository_uri"
            sys.exit(1)
        remote_repo=arguments[2]
        return action_sync(repo_location,remote_repo)
    elif action=='generate-metadata':
        return action_generate_metadata(repo_location)
    elif action=='list-categories':
        return list_categories(repo_location)
    elif action=='list-packages':
        return list_packages(repo_location)
    elif action=='package':
        if len(arguments)<3:
            print "The 'package' action takes the following parameters:"
            print " * category/package_name"
            print " * [version]"
            sys.exit(1)
        package_name=arguments[2]
        return action_package(repo_location,package_name)
    elif action=='usage':
        return usage()
    elif action in pms_phases and hasattr(phases,action):
        return getattr(phases,action)(os.environ,repo_location)
    elif action in actions_wanted:
        raise NotImplementedError
    else:
        return usage()

if __name__ == "__main__":
    sys.exit(main())