def tsGetStatus(theFile): ''' Return the output of "ls -ld theFile" as a string. This function uses the getoutput() function, and properly escapes backslashes and dollar signs in the argument. ''' try: return subprocess.getstatus(theFile) except: return None
# print(sub2.pid, 'term', ret1, ret1) # break # i += 1 # # !!注意:当我们直接用cmd而非cmd_list时,得到的pid并不是pdf2html起的进程,而是其父进程,切记切记 # args = '' # subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, # close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, # creationflags=0) # 可以执行shell命令的相关模块和函数有: # os.system # os.spawn print(subprocess.getstatusoutput('dir')) # (0, '/home/ronny') print(subprocess.getoutput('dir')) # '/home/ronny' print(subprocess.getstatus('dir')) ret1 = subprocess.call("ifconfig") ret2 = subprocess.call("ipconfig") print(ret1) # 0 print(ret2) # 1 ret = subprocess.call(["ls", "-l"], shell=False) # shell为False的时候命令必须分开写 ret = subprocess.call("ls -l", shell=True) subprocess.check_call(["ls", "-l"]) subprocess.check_call("exit 1", shell=True) subprocess.check_output(["echo", "Hello World!"]) subprocess.check_output("exit 1", shell=True) (4) # subprocess.Popen(...)#用于执行复杂的系统命令 ret1 = subprocess.Popen(["mkdir", "t1"])
def configure(advanced): # This will be called by supybot to configure this module. advanced is # a bool that specifies whether the user identified himself as an advanced # user or not. You should effect your configuration by manipulating the # registry as appropriate. def makeSource(release): return """deb http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse """ % (release, release) #""" from supybot.questions import output, expect, something, yn import subprocess import os def anything(prompt, default=None): """Because supybot is pure fail""" from supybot.questions import expect return expect(prompt, [], default=default) PackageInfo = conf.registerPlugin('PackageInfo', True) enabled = yn("Enable this plugin in all channels?", default=True) if enabled and advanced: prefixchar = something("Which prefix character should be bot respond to?", default=PackageInfo.prefixchar._default) defaultRelease = something("What should be the default distrobution when not specified?", default=PackageInfo.defaultRelease._default) aptdir = something("Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default) # People tend to thing this should be /var/cache/apt while aptdir.startswith('/var'): #NOTE: This is not a good hack. Maybe just blacklist /var/cache/apt (or use apt to report back the cache dir) output("NO! Do not use your systems apt directory") aptdir = something("Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default) else: prefixchar = PackageInfo.prefixchar._default defaultRelease = PackageInfo.defaultRelease._default aptdir = PackageInfo.aptdir._default PackageInfo.enabled.setValue(enabled) PackageInfo.aptdir.setValue(aptdir) PackageInfo.prefixchar.setValue(prefixchar) PackageInfo.defaultRelease.setValue(defaultRelease) default_dists = set(['dapper', 'hardy', 'lucid', 'maveric', 'natty', 'oneiric']) pluginDir = os.path.abspath(os.path.dirname(__file__)) update_apt = os.path.join(pluginDir, 'update_apt') update_apt_file = os.path.join(pluginDir, 'update_apt_file') default_dists.add(defaultRelease) ## Create the aptdir try: os.makedirs(aptdir) except OSError: # The error number would be OS dependant (17 on Linux 2.6, ?? on others). So just pass on this pass for release in default_dists: filename = os.path.join(aptdir, "%s.list" % release) try: output("Creating %s" % filename) fd = open(filename, 'wb') fd.write("# Apt sources list for Ubuntu %s\n" % release) fd.write(makeSource(release)) fd.write(makeSource(release + '-security')) fd.write(makeSource(release + '-updates')) fd.close() for sub in ('backports', 'proposed'): sub_release = "%s-%s" % (release, sub) filename = os.path.join(aptdir, "%s.list" % sub_release) output("Creating %s" % filename) fd = open(filename, 'wb') fd.write("# Apt sources list for Ubuntu %s\n" % release) fd.write(makeSource(sub_release)) fd.close() except Exception as e: output("Error writing to %r: %r (%s)" % (filename, str(e), type(e))) if yn("In order for the plugin to use these sources, you must run the 'update_apt' script, do you want to do this now?", default=True): os.environ['DIR'] = aptdir # the update_apt script checks if DIR is set and uses it if it is if subprocess.getstatus(update_apt) != 0: output("There was an error running update_apt, please run '%s -v' to get more information" % update_apt) if subprocess.getstatusoutput('which apt-file') != 0: output("You need to install apt-file in order to use the !find command of this plugin") else: if yn("In order for the !find command to work, you must run the 'update_apt_file' script, do you want to do this now?", default=True): os.environ['DIR'] = aptdir # the update_apt_file script checks if DIR is set and uses it if it is if subprocess.getstatusoutput(update_apt_file) != 0: output("There was an error running update_apt_file, please run '%s -v' to get more information" % update_apt_file)
def configure(advanced): # This will be called by supybot to configure this module. advanced is # a bool that specifies whether the user identified himself as an advanced # user or not. You should effect your configuration by manipulating the # registry as appropriate. def makeSource(release): return """deb http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse deb-src http://archive.ubuntu.com/ubuntu/ %s main restricted universe multiverse """ % (release, release) #""" from supybot.questions import output, expect, something, yn import subprocess import os def anything(prompt, default=None): """Because supybot is pure fail""" from supybot.questions import expect return expect(prompt, [], default=default) PackageInfo = conf.registerPlugin('PackageInfo', True) enabled = yn("Enable this plugin in all channels?", default=True) if enabled and advanced: prefixchar = something( "Which prefix character should be bot respond to?", default=PackageInfo.prefixchar._default) defaultRelease = something( "What should be the default distrobution when not specified?", default=PackageInfo.defaultRelease._default) aptdir = something( "Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default) # People tend to thing this should be /var/cache/apt while aptdir.startswith( '/var' ): #NOTE: This is not a good hack. Maybe just blacklist /var/cache/apt (or use apt to report back the cache dir) output("NO! Do not use your systems apt directory") aptdir = something( "Which directory should be used for the apt cache when looking up packages?", default=PackageInfo.aptdir._default) else: prefixchar = PackageInfo.prefixchar._default defaultRelease = PackageInfo.defaultRelease._default aptdir = PackageInfo.aptdir._default PackageInfo.enabled.setValue(enabled) PackageInfo.aptdir.setValue(aptdir) PackageInfo.prefixchar.setValue(prefixchar) PackageInfo.defaultRelease.setValue(defaultRelease) default_dists = set( ['dapper', 'hardy', 'lucid', 'maveric', 'natty', 'oneiric']) pluginDir = os.path.abspath(os.path.dirname(__file__)) update_apt = os.path.join(pluginDir, 'update_apt') update_apt_file = os.path.join(pluginDir, 'update_apt_file') default_dists.add(defaultRelease) ## Create the aptdir try: os.makedirs(aptdir) except OSError: # The error number would be OS dependant (17 on Linux 2.6, ?? on others). So just pass on this pass for release in default_dists: filename = os.path.join(aptdir, "%s.list" % release) try: output("Creating %s" % filename) fd = open(filename, 'wb') fd.write("# Apt sources list for Ubuntu %s\n" % release) fd.write(makeSource(release)) fd.write(makeSource(release + '-security')) fd.write(makeSource(release + '-updates')) fd.close() for sub in ('backports', 'proposed'): sub_release = "%s-%s" % (release, sub) filename = os.path.join(aptdir, "%s.list" % sub_release) output("Creating %s" % filename) fd = open(filename, 'wb') fd.write("# Apt sources list for Ubuntu %s\n" % release) fd.write(makeSource(sub_release)) fd.close() except Exception as e: output("Error writing to %r: %r (%s)" % (filename, str(e), type(e))) if yn("In order for the plugin to use these sources, you must run the 'update_apt' script, do you want to do this now?", default=True): os.environ[ 'DIR'] = aptdir # the update_apt script checks if DIR is set and uses it if it is if subprocess.getstatus(update_apt) != 0: output( "There was an error running update_apt, please run '%s -v' to get more information" % update_apt) if subprocess.getstatusoutput('which apt-file') != 0: output( "You need to install apt-file in order to use the !find command of this plugin" ) else: if yn("In order for the !find command to work, you must run the 'update_apt_file' script, do you want to do this now?", default=True): os.environ[ 'DIR'] = aptdir # the update_apt_file script checks if DIR is set and uses it if it is if subprocess.getstatusoutput(update_apt_file) != 0: output( "There was an error running update_apt_file, please run '%s -v' to get more information" % update_apt_file)