summaryrefslogtreecommitdiff
blob: 90fecfad9b1e6920021ef08bf5a62d19eac420cd (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python
import getopt, sys, os, string, urllib, re, subprocess
import settings

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

def verbose_system(command):
    print command
    return os.system(command)

def list_configured_drivers():
    #all .cfg files which end in .cfg as they should
    return [x[:-len('.cfg')] for x in os.listdir(settings.GLOBAL_CONF_DIR) if x[-len('.cfg'):]=='.cfg']

#read key=value file to dict
def read_config(conf_file,defaults={}):
    conffile=open(conf_file)
    conf=defaults.copy()
    for line in conffile:
        if len(line):
            value=line[line.find("=")+1:]
            if value[-1]=='\n':
                value=value[:-1]
            conf[line[:line.find("=")]]=value

    return conf

#returns dict of key=value config file
def read_driver_config(driver_name):
    conffile=os.path.join(settings.GLOBAL_CONF_DIR,settings.DRIVER_DIR,driver_name+'.cfg')
    return read_config(conffile,{
        #'name':None,
        #'executable':None,
        })
    return configs #dict

#read g-common config for a repo
def read_repo_config(repo_location):
    hidden_conffile=os.path.join(repo_location,settings.MYDIR,'repo.cfg')
    return read_config(hidden_conffile)

#sync a local repository's PACKAGES file
def action_sync(repo_location,driver,remote_uri):
    if driver==None:
        repo_conf=read_repo_config(repo_location)
        driver=repo_conf['driver']
    driver_conf=read_driver_config(driver)
    
    if remote_uri is None:
        remote_uri=repo_conf['uri']
    
    if os.path.exists(repo_location):
        try:
            os.makedirs(os.path.join(repo_location,settings.MYDIR))
        except:
            pass
    cfg_file=open(os.path.join(repo_location,settings.MYDIR,"repo.cfg"),"w")
    cfg_file.write('driver='+driver+'\n')
    cfg_file.write('uri='+remote_uri+'\n')
    cfg_file.close()

    return verbose_system(driver_conf['exec']+" "+repo_location+" sync "+remote_uri)

#list categories in this repositorie
def list_categories(repo_location):
    repo_conf=read_repo_config(repo_location)
    driver_conf=read_driver_config(repo_conf['driver'])
    
    if remote_uri is None:
        remote_uri=repo_conf['uri']

    return verbose_system(driver_conf['exec']+" "+repo_location+" list-categories")

#idem ditto
def list_packages(repo_location):
    repo_conf=read_repo_config(repo_location)
    driver_conf=read_driver_config(repo_conf['driver'])
    
    if remote_uri is None:
        remote_uri=repo_conf['uri']

    return verbose_system(driver_conf['exec']+" "+repo_location+" list-packages")


#generate a tree of ebuilds... note that we only link ebuild files, instead of generating them
#we will, however, generate metadata.xml and Manifest files
def generate_tree(repo_location):
    repo_conf=read_repo_config(repo_location)
    driver_conf=read_driver_config(repo_conf['driver'])

    ebuild_file=settings.COMMON_EBUILD_FILE #get from settings
    import hashlib
    ebuild_digest=hashlib.sha1(open(ebuild_file).read()).hexdigest()
    Manifest="EBUILD %s SHA1 "+ebuild_digest

    packages_list_pipe=subprocess.Popen(driver_conf['exec']+' '+repo_location+' list-packages',shell=True,stdout=subprocess.PIPE)
    os.waitpid(packages_list_pipe.pid,0)
    for line in packages_list_pipe.stdout:
        category=line[:line.find("/")]
        package=line[line.find("/")+1:line.find(" ")]
        version=line[line.find(" ")+1:-1]
        ebuild_dir=os.path.join(repo_location,category,package)
        if not os.path.exists(ebuild_dir):
            os.makedirs(ebuild_dir)
        package_file=package+'-'+version+'.ebuild'
        os.symlink(ebuild_file,os.path.join(ebuild_dir,package_file))
        manifest_file=open(os.path.join(ebuild_dir,'Manifest'),"w")
        manifest_file.write(Manifest % package_file)

#list package details, in PMS's format
def action_package(repo_location,package_name):
    repo_conf=read_repo_config(repo_location)
    driver_conf=read_driver_config(repo_conf['driver'])
    
    if remote_uri is None:
        remote_uri=repo_conf['uri']

    return verbose_system(driver_conf['exec']+" "+repo_location+" package "+package_name)

def usage():
    print __doc__

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)<2 or 'help' in arguments:
            print "The 'sync' action takes the following parameters:"
            print " * [driver]"
            print " * [remote_repository_uri]"
            sys.exit(1)
        driver=None
        remote_repo=None
        if len(arguments)>2:
            driver=arguments[2]
        if len(arguments)>3:
            remote_repo=arguments[3]
        action_sync(repo_location,driver,remote_repo)
    elif action=='list-categories':
        list_categories(repo_location)
    elif action=='list-packages':
        list_packages(repo_location)
    elif action=='generate-tree':
        generate_tree(repo_location)
    elif action=='package':
        if len(arguments)<3 or 'help' in arguments:
            print "The 'package' action takes the following parameters:"
            print " * category/package_name"
            print " * [version]"
            sys.exit(1)
        package_name=arguments[2]
        action_package(repo_location,package_name)
    elif action=='usage' or action=='help':
        usage()
    elif action in pms_phases:
        #todo
        raise NotImplementedError
    elif action in actions_wanted:
        raise NotImplementedError
    else:
        usage()
    sys.exit(0)

if __name__ == "__main__":
    main()