rl_ci_tools.py
changeset 5 539b3d5f515e
parent 4 1899f4fe9c9b
child 6 2fe9dd0a6b76
equal deleted inserted replaced
4:1899f4fe9c9b 5:539b3d5f515e
    65 
    65 
    66     def download(self,u,p,kind, fn, dst):
    66     def download(self,u,p,kind, fn, dst):
    67         for i in self.info(u,p,kind[:-1],fn):
    67         for i in self.info(u,p,kind[:-1],fn):
    68             self._download(u,p,kind,i[0],dst)
    68             self._download(u,p,kind,i[0],dst)
    69 
    69 
    70     def info(self,u,p,kind,pat):
    70     def info(self,u,p,kind,pat,subdir=''):
    71         #self.login(u,p)
    71         #self.login(u,p)
    72         url = '%s/pypi/%s-info/%s/?json=1' % (self.root,kind,pat)
    72         if subdir:
       
    73             subdir += '/'
       
    74         url = '%s/pypi/%s-info/%s%s/?json=1' % (self.root,kind,subdir,pat)
    73         resp = self.session.get(url,
    75         resp = self.session.get(url,
    74                 #data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
    76                 #data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
    75                 headers = dict(Referer=self.loginurl),
    77                 headers = dict(Referer=self.loginurl),
    76                 )
    78                 )
    77         status_code = resp.status_code
    79         status_code = resp.status_code
    83         I = json.loads(b)
    85         I = json.loads(b)
    84         if verbosity>1:
    86         if verbosity>1:
    85             print('%s: %r --> %d rows' % (PROG, url, len(I)))
    87             print('%s: %r --> %d rows' % (PROG, url, len(I)))
    86         return I
    88         return I
    87 
    89 
    88     def upload(self,u,p,kind,fn):
    90     def upload(self,u,p,kind,fn,subdir=''):
    89         self.login(u,p)
    91         self.login(u,p)
    90         url = '%s/pypi/upload-%s/' % (self.root,kind)
    92         if subdir:
       
    93             subdir = '/' + subdir
       
    94         url = '%s/pypi/upload-%s%s/' % (self.root,kind,subdir)
    91         files= dict(file=(os.path.basename(fn),open(fn,'rb'),'application/octet-stream'))
    95         files= dict(file=(os.path.basename(fn),open(fn,'rb'),'application/octet-stream'))
    92         resp = self.session.post(url,
    96         resp = self.session.post(url,
    93                 data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
    97                 data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
    94                 files=files,
    98                 files=files,
    95                 headers = dict(Referer=self.loginurl),
    99                 headers = dict(Referer=self.loginurl),
    98         text = resp.text
   102         text = resp.text
    99         if text!='OK' or status_code!=200:
   103         if text!='OK' or status_code!=200:
   100             raise ValueError('%s: upload %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
   104             raise ValueError('%s: upload %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
   101         if verbosity:
   105         if verbosity:
   102             print('%s: uploaded %r to %r.' % (PROG,fn,url))
   106             print('%s: uploaded %r to %r.' % (PROG,fn,url))
       
   107         return resp.status_code
       
   108 
       
   109     def clear_cache(self,u,p,fn):
       
   110         self.login(u,p)
       
   111         url = '%s/pypi/clear-cache/%s/' % (self.root,fn)
       
   112         resp = self.session.post(url,
       
   113                 data=dict(csrfmiddlewaretoken=self.session.cookies['csrftoken']),
       
   114                 headers = dict(Referer=self.loginurl),
       
   115                 )
       
   116         status_code = resp.status_code
       
   117         text = resp.text
       
   118         if not text.endswith('OK') or status_code!=200:
       
   119             raise ValueError('%s: clear-cache %r failed with status_code=%r!\n%r' % (PROG,url,status_code,text))
       
   120         if verbosity:
       
   121             print('%s: cleared cache %r.' % (PROG,fn))
   103         return resp.status_code
   122         return resp.status_code
   104 
   123 
   105     def package_version(self,u,p,pkg):
   124     def package_version(self,u,p,pkg):
   106         I = self.info(u,p,'package','%s-*' % pkg)
   125         I = self.info(u,p,'package','%s-*' % pkg)
   107         if not I:
   126         if not I:
   209             status_code = pypi.login(u,p)
   228             status_code = pypi.login(u,p)
   210             if debug:
   229             if debug:
   211                 print('status=%s' % status_code)
   230                 print('status=%s' % status_code)
   212         elif cmd.startswith('download-'):
   231         elif cmd.startswith('download-'):
   213             kind = cmd.split('-')[1]
   232             kind = cmd.split('-')[1]
   214             if not kind in ('resources','packages'):
   233             if not kind in ('resources','packages', 'caches'):
   215                 raise ValueError('%s: invalid download kind: %r' % (PROG,kind))
   234                 raise ValueError('%s: invalid download kind: %r' % (PROG,kind))
   216             if dst and not os.path.isdir(dst):
   235             if dst and not os.path.isdir(dst):
   217                 raise ValueError('%s: %r is not a directory!' % (PROG,dst))
   236                 raise ValueError('%s: %r is not a directory!' % (PROG,dst))
   218             for fn in sys.argv[2:]:
   237             for fn in sys.argv[2:]:
   219                 pypi.download(u,p,kind,fn,dst)
   238                 pypi.download(u,p,kind,fn,dst)
   220         elif cmd.endswith('-info'):
   239         elif cmd.endswith('-info'):
   221             kind = cmd.split('-')[0]
   240             kind = cmd.split('-')[0]
   222             if not kind in ('resource','package'):
   241             if not kind in ('resource','package', 'cache'):
   223                 raise ValueError('%s: invalid info kind: %r' % (PROG,kind))
   242                 raise ValueError('%s: invalid info kind: %r' % (PROG,kind))
   224             tabulate([i for fn in sys.argv[2:] for i in pypi.info(u,p,kind,fn)])
   243             subdir = getoption('subdir','',str) if kind=='cache' else None
       
   244             tabulate([i for fn in sys.argv[2:] for i in pypi.info(u,p,kind,fn,subdir)])
   225         elif cmd.startswith('upload-'):
   245         elif cmd.startswith('upload-'):
   226             kind = cmd.split('-')[1]
   246             kind = cmd.split('-')[1]
   227             if not kind in ('resources','packages'):
   247             if not kind in ('resources','packages', 'caches'):
   228                 raise ValueError('%s: invalid upload kind: %r' % (PROG,kind))
   248                 raise ValueError('%s: invalid upload kind: %r' % (PROG,kind))
       
   249             subdir = getoption('subdir','',str) if kind=='caches' else None
   229             for pat in sys.argv[2:]:
   250             for pat in sys.argv[2:]:
   230                 for fn in glob.glob(pat):
   251                 for fn in glob.glob(pat):
   231                     pypi.upload(u,p,kind[:-1],fn)
   252                     pypi.upload(u,p,kind[:-1],fn,subdir)
       
   253         elif cmd=='clear-cache':
       
   254             for fn in sys.argv[2:]:
       
   255                 pypi.clear_cache(u,p,fn)
   232         elif cmd=='package-version':
   256         elif cmd=='package-version':
   233             tabulate([pypi.package_version(u,p,fn) for fn in sys.argv[2:]],
   257             tabulate([pypi.package_version(u,p,fn) for fn in sys.argv[2:]],
   234                     hdrs = ['Package','Version'],
   258                     hdrs = ['Package','Version'],
   235                     fmtmpl8 = '{:<%d}  {:>%d}',
   259                     fmtmpl8 = '{:<%d}  {:>%d}',
   236                     cnvf = (str,str),
   260                     cnvf = (str,str),
   237                     )
   261                     )
   238         else:
   262         else:
   239             raise ValueError('%s: nknown command %r' % (PROG,cmd))
   263             raise ValueError('%s: unknown command %r' % (PROG,cmd))
   240 if __name__=='__main__':
   264 if __name__=='__main__':
   241     main()
   265     main()