1""" 2Support for managing apps (as created with "0install add"). 3@since: 1.9 4""" 5 6# Copyright (C) 2012, Thomas Leonard 7# See the README file for details, or visit http://0install.net. 8 9fromzeroinstallimport_,SafeException,logger 10fromzeroinstall.supportimportbasedir,portable_rename 11fromzeroinstall.injectorimportnamespaces,selections,qdom,model 12importre,os,time,tempfile 13 14# Avoid characters that are likely to cause problems (reject : and ; everywhere 15# so that apps can be portable between POSIX and Windows). 16valid_name=re.compile(r'''^[^./\\:=;'"][^/\\:=;'"]*$''') 17
19"""@type name: str""" 20ifname=='0install': 21raiseSafeException("Creating an app called '0install' would cause trouble; try e.g. '00install' instead") 22ifvalid_name.match(name):return 23raiseSafeException("Invalid application name '{name}'".format(name=name))
26"""Try to guess the command to set an environment variable.""" 27shell=os.environ.get('SHELL','?') 28if'csh'inshell: 29return"setenv %s %s"%(name,value) 30return"export %s=%s"%(name,value)
33"""Find the first writable path in the list (default $PATH), 34 skipping /bin, /sbin and everything under /usr except /usr/local/bin 35 @type paths: [str] | None 36 @rtype: str""" 37ifpathsisNone: 38paths=os.environ['PATH'].split(os.pathsep) 39forpathinpaths: 40ifpath.startswith('/usr/')andnotpath.startswith('/usr/local/bin'): 41# (/usr/local/bin is OK if we're running as root) 42pass 43elifpath.startswith('/bin')orpath.startswith('/sbin'): 44pass 45elifos.path.realpath(path).startswith(basedir.xdg_cache_home): 46pass# print "Skipping cache", first_path 47elifnotos.access(path,os.W_OK): 48pass# print "No access", first_path 49else: 50break 51else: 52path=os.path.expanduser('~/bin/') 53logger.warning('%s is not in $PATH. Add it with:\n%s'%(path,_export('PATH',path+':$PATH'))) 54 55ifnotos.path.isdir(path): 56os.makedirs(path) 57returnpath
58 59_command_template="""#!/bin/sh 60exec 0install run {app} "$@" 61""" 62
69"""If stream is a shell script for an application, return the app details. 70 @param stream: the executable file's stream (will seek) 71 @type stream: file-like object 72 @return: the app details, if any 73 @rtype: L{AppScriptInfo} | None 74 @since: 1.12""" 75try: 76stream.seek(0) 77template_header=_command_template[:_command_template.index("{app}")] 78actual_header=stream.read(len(template_header)) 79stream.seek(0) 80iftemplate_header==actual_header: 81# If it's a launcher script, it should be quite short! 82rest=stream.read() 83line=rest.split('\n')[1] 84else: 85returnNone 86exceptUnicodeDecodeErrorasex: 87logger.info("Not an app script '%s': %s",stream,ex) 88returnNone 89 90info=AppScriptInfo() 91info.name=line.split()[3] 92returninfo
101"""Store a new set of selections. We include today's date in the filename102 so that we keep a history of previous selections (max one per day), in case103 we want to to roll back later.104 @type sels: L{zeroinstall.injector.selections.Selections}105 @type set_last_checked: bool"""106date=time.strftime('%Y-%m-%d')107sels_file=os.path.join(self.path,'selections-{date}.xml'.format(date=date))108dom=sels.toDOM()109110ifself.config.handler.dry_run:111print(_("[dry-run] would write selections to {file}").format(file=sels_file))112else:113tmp=tempfile.NamedTemporaryFile(prefix='selections.xml-',dir=self.path,delete=False,mode='wt')114try:115dom.writexml(tmp,addindent=" ",newl="\n",encoding='utf-8')116except:117tmp.close()118os.unlink(tmp.name)119raise120tmp.close()121portable_rename(tmp.name,sels_file)122123sels_latest=os.path.join(self.path,'selections.xml')124ifself.config.handler.dry_run:125print(_("[dry-run] would update {link} to point to new selections file").format(link=sels_latest))126else:127ifos.path.exists(sels_latest):128os.unlink(sels_latest)129ifos.name=="nt":130importshutil131shutil.copyfile(sels_file,sels_latest)132else:133os.symlink(os.path.basename(sels_file),sels_latest)134135ifset_last_checked:136self.set_last_checked()
139"""Load the selections.140 If may_update is True then the returned selections will be cached and available.141 @param snapshot_date: get a historical snapshot142 @type snapshot_date: (as returned by L{get_history}) | None143 @param may_update: whether to check for updates144 @type may_update: bool145 @param use_gui: whether to use the GUI for foreground updates146 @type use_gui: bool | None (never/always/if possible)147 @return: the selections148 @rtype: L{selections.Selections}"""149ifsnapshot_date:150assertmay_updateisFalse,"Can't update a snapshot!"151sels_file=os.path.join(self.path,'selections-'+snapshot_date+'.xml')152else:153sels_file=os.path.join(self.path,'selections.xml')154withopen(sels_file,'rb')asstream:155sels=selections.Selections(qdom.parse(stream))156157ifmay_update:158sels=self._check_for_updates(sels,use_gui)159160returnsels
163"""Get the dates of the available snapshots, starting with the most recent.164 @rtype: [str]"""165date_re=re.compile('selections-(\d\d\d\d-\d\d-\d\d).xml')166snapshots=[]167forfinos.listdir(self.path):168match=date_re.match(f)169ifmatch:170snapshots.append(match.group(1))171snapshots.sort(reverse=True)172returnsnapshots
175"""Download any missing implementations.176 @type sels: L{zeroinstall.injector.selections.Selections}177 @return: a blocker which resolves when all needed implementations are available178 @rtype: L{tasks.Blocker} | None"""179returnsels.download_missing(self.config)# TODO: package impls
182"""Check whether the selections need to be updated.183 If any input feeds have changed, we re-run the solver. If the184 new selections require a download, we schedule one in the185 background and return the old selections. Otherwise, we return the186 new selections. If we can select better versions without downloading,187 we update the app's selections and return the new selections.188 If we can't use the current selections, we update in the foreground.189 We also schedule a background update from time-to-time anyway.190 @type sels: L{zeroinstall.injector.selections.Selections}191 @type use_gui: bool192 @return: the selections to use193 @rtype: L{selections.Selections}"""194need_solve=False# Rerun solver (cached feeds have changed)195need_update=False# Update over the network196197utime=self._get_mtime('last-checked',warn_if_missing=True)198last_solve=max(self._get_mtime('last-solve',warn_if_missing=False),utime)199200# Ideally, this would return all the files which were inputs into the solver's201# decision. Currently, we approximate with:202# - the previously selected feed files (local or cached)203# - configuration files for the selected interfaces204# - the global configuration205# We currently ignore feeds and interfaces which were206# considered but not selected.207# Can yield None (ignored), paths or (path, mtime) tuples.208# If this throws an exception, we will log it and resolve anyway.209defget_inputs():210forselinsels.selections.values():211logger.info("Checking %s",sel.feed)212feed=iface_cache.get_feed(sel.feed)213ifnotfeed:214raiseIOError("Input %s missing; update"%sel.feed)215else:216iffeed.local_path:217yieldfeed.local_path218else:219yield(feed.url,feed.last_modified)220221# Per-feed configuration222yieldbasedir.load_first_config(namespaces.config_site,namespaces.config_prog,223'interfaces',model._pretty_escape(sel.interface))224225# Global configuration226yieldbasedir.load_first_config(namespaces.config_site,namespaces.config_prog,'global')
227228# If any of the feeds we used have been updated since the last check, do a quick re-solve229iface_cache=self.config.iface_cache230try:231foriteminget_inputs():232ifnotitem:continue233ifisinstance(item,tuple):234path,mtime=item235else:236path=item237try:238mtime=os.stat(path).st_mtime239exceptOSErrorasex:240logger.info("Triggering update to {app} due to error: {ex}".format(241app=self,path=path,ex=ex))242need_solve=True243break244245ifmtimeandmtime>last_solve:246logger.info("Triggering update to %s because %s has changed",self,path)247need_solve=True248break249exceptExceptionasex:250logger.info("Error checking modification times: %s",ex)251need_solve=True252need_update=True253254# Is it time for a background update anyway?255ifnotneed_update:256staleness=time.time()-utime257logger.info("Staleness of app %s is %d hours",self,staleness/(60*60))258freshness_threshold=self.config.freshness259iffreshness_threshold>0andstaleness>=freshness_threshold:260need_update=True261262# If any of the saved selections aren't available then we need263# to download right now, not later in the background.264unavailable_selections=sels.get_unavailable_selections(config=self.config,include_packages=True)265ifunavailable_selections:266logger.info("Saved selections are unusable (missing %s)",267', '.join(str(s)forsinunavailable_selections))268need_solve=True269270ifneed_solve:271fromzeroinstall.injector.driverimportDriver272driver=Driver(config=self.config,requirements=self.get_requirements())273ifdriver.need_download():274ifunavailable_selections:275returnself._foreground_update(driver,use_gui)276else:277# Continue with the current (cached) selections while we download278need_update=True279else:280old_sels=sels281sels=driver.solver.selections282fromzeroinstall.supportimportxmltools283ifnotxmltools.nodes_equal(sels.toDOM(),old_sels.toDOM()):284self.set_selections(sels,set_last_checked=False)285try:286self._touch('last-solve')287exceptOSErrorasex:288logger.warning("Error checking for updates: %s",ex)289290# If we tried to check within the last hour, don't try again.291ifneed_update:292last_check_attempt=self._get_mtime('last-check-attempt',warn_if_missing=False)293iflast_check_attemptandlast_check_attempt+60*60>time.time():294logger.info("Tried to check within last hour; not trying again now")295need_update=False296297ifneed_update:298try:299self.set_last_check_attempt()300exceptOSErrorasex:301logger.warning("Error checking for updates: %s",ex)302else:303fromzeroinstall.injectorimportbackground304r=self.get_requirements()305background.spawn_background_update2(r,False,self)306307returnsels
380"""@type name: str"""381timestamp_path=os.path.join(self.path,name)382ifself.config.handler.dry_run:383pass#print(_("[dry-run] would update timestamp file {file}").format(file = timestamp_path))384else:385fd=os.open(timestamp_path,os.O_WRONLY|os.O_CREAT,0o644)386os.close(fd)387os.utime(timestamp_path,None)# In case file already exists
390"""@type name: str391 @type warn_if_missing: bool392 @rtype: int"""393timestamp_path=os.path.join(self.path,name)394try:395returnos.stat(timestamp_path).st_mtime396exceptExceptionasex:397ifwarn_if_missing:398logger.warning("Failed to get time-stamp of %s: %s",timestamp_path,ex)399return0
402"""Get the time of the last successful check for updates.403 @return: the timestamp (or None on error)404 @rtype: float | None"""405returnself._get_mtime('last-checked',warn_if_missing=True)
408"""Get the time of the last attempted check.409 @return: the timestamp, or None if we updated successfully.410 @rtype: float | None"""411last_check_attempt=self._get_mtime('last-check-attempt',warn_if_missing=False)412iflast_check_attempt:413last_checked=self.get_last_checked()414415iflast_checked<last_check_attempt:416returnlast_check_attempt417returnNone
420# Check for shell command421# TODO: remember which commands we own instead of guessing422name=self.get_name()423bin_dir=find_bin_dir()424launcher=os.path.join(bin_dir,name)425expanded_template=_command_template.format(app=name)426ifos.path.exists(launcher)andos.path.getsize(launcher)==len(expanded_template):427withopen(launcher,'r')asstream:428contents=stream.read()429ifcontents==expanded_template:430ifself.config.handler.dry_run:431print(_("[dry-run] would delete launcher script {file}").format(file=launcher))432else:433os.unlink(launcher)434435ifself.config.handler.dry_run:436print(_("[dry-run] would delete directory {path}").format(path=self.path))437else:438# Remove the app itself439importshutil440shutil.rmtree(self.path)
443# TODO: remember which commands we create444"""@type name: str"""445ifnotvalid_name.match(name):446raiseSafeException("Invalid shell command name '{name}'".format(name=name))447bin_dir=find_bin_dir()448launcher=os.path.join(bin_dir,name)449ifos.path.exists(launcher):450raiseSafeException("Command already exists: {path}".format(path=launcher))451452ifself.config.handler.dry_run:453print(_("[dry-run] would write launcher script {path}").format(path=launcher))454else:455withopen(launcher,'w')asstream:456stream.write(_command_template.format(app=self.get_name()))457# Make new script executable458os.chmod(launcher,0o111|os.fstat(stream.fileno()).st_mode)
496"""Get the App for name.497 Returns None if name is not an application (doesn't exist or is not a valid name).498 Since / and : are not valid name characters, it is generally safe to try this499 before calling L{injector.model.canonical_iface_uri}.500 @type name: str501 @type missing_ok: bool502 @rtype: L{App}"""503ifnotvalid_name.match(name):504ifmissing_ok:505returnNone506else:507raiseSafeException("Invalid application name '{name}'".format(name=name))508app_dir=basedir.load_first_config(namespaces.config_site,"apps",name)509ifapp_dir:510returnApp(self.config,app_dir)511ifmissing_ok:512returnNone513else:514raiseSafeException("No such application '{name}'".format(name=name))