在创建 Python 包时,我需要在“sdist”阶段运行我自己的脚本。 我写了以下脚本。你知道更好的方法吗?您能否推荐更好的一个或链接到说明这一刻的 setuptools 官方文档?
import subprocess
import sys
from setuptools import setup, find_packages, os
if 'sdist' in sys.argv:
cwd = os.getcwd()
os.chdir('website/static/stylesheets/')
result = subprocess.call("scss --update --compass ./*.scss", shell=True)
if result != 0:
exit(1)
os.chdir(cwd)
setup(name = "site",
author="Vladimir Ignatev",
author_email="mail@gmail.com",
version="0.1",
packages=find_packages(),
include_package_data=True,
zip_safe=True,
)
最佳答案
虽然来晚了,但这里有一个解决方案。
基本上,它只是继承 distutils 的 sdist 命令,添加自定义逻辑并将其注册到设置函数中。 Unfortunately, the official documentation to this topic is kind of vague and laconic ; Extending Distutils至少为开始提供了一个小例子。我发现阅读 distutils.command 包中的模块代码以了解实际命令的实现方式要好得多。
要执行任意命令,您可以使用方法 distutils.cmd.Command::spawn 执行传递的输入字符串,如果命令的退出代码引发 DistutilsExecError不为零:
from distutils.command.sdist import sdist as sdist_orig
from distutils.errors import DistutilsExecError
from setuptools import setup
class sdist(sdist_orig):
def run(self):
try:
self.spawn(['ls', '-l'])
except DistutilsExecError:
self.warn('listing directory failed')
super().run()
setup(name='spam',
version='0.1',
packages=[],
cmdclass={
'sdist': sdist
}
)
运行上面的安装脚本会产生:
$ python setup.py sdist
running sdist
ls -l
total 24
-rw-r--r-- 1 hoefling staff 52 23 Dez 19:06 MANIFEST
drwxr-xr-x 3 hoefling staff 96 23 Dez 19:06 dist
-rw-r--r-- 1 hoefling staff 484 23 Dez 19:07 setup.py
running check
...
writing manifest file 'MANIFEST'
creating spam-0.1
making hard links in spam-0.1...
hard linking setup.py -> spam-0.1
Creating tar archive
removing 'spam-0.1' (and everything under it)
这是(尽管已简化)我们在项目中使用的命令的真实示例,该命令围绕 NodeJS 项目使用并调用 yarn:
import distutils
import os
import pathlib
import setuptools
_YARN_CMD_SEP = ';'
_HELP_MSG_SUBCMD = (
'yarn subcommands to execute (separated '
'by {})'.format(_YARN_CMD_SEP)
)
_HELP_MSG_PREFIX = (
'path to directory containing package.json. '
'If not set, current directory is assumed.'
)
class yarn(setuptools.Command):
description = ('runs yarn commands. Assumes yarn is '
'already installed by the user.')
user_options = [
('subcommands=', None, _HELP_MSG_SUBCMD),
('prefix=', None, _HELP_MSG_PREFIX),
]
def initialize_options(self) -> None:
self.subcommands = []
self.prefix = None # type: pathlib.Path
def finalize_options(self) -> None:
self.subcommands = [
cmd.strip() for cmd in str(self.subcommands).split(self._YARN_CMD_SEP)
]
self.prefix = pathlib.Path(self.prefix) if self.prefix else pathlib.Path()
def run(self) -> None:
cwd = pathlib.Path().absolute()
os.chdir(str(self.prefix.absolute())) # change to prefix dir
for cmd in self.subcommands:
self.announce('running yarn {} ...'.format(cmd), level=distutils.log.INFO)
self.spawn(['yarn'] + cmd.split(' '))
os.chdir(str(cwd)) # change back to our previous dir
示例用法:
$ python setup.py yarn --prefix=. --subcommands="add leftpad; remove leftpad"
running yarn
running yarn add leftpad ...
yarn add leftpad
yarn add v1.3.2
warning package.json: No license field
warning No license field
[1/4] ? Resolving packages...
[2/4] ? Fetching packages...
[3/4] ? Linking dependencies...
[4/4] ? Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
└─ leftpad@0.0.1
warning No license field
✨ Done in 0.33s.
running yarn remove leftpad ...
yarn remove leftpad
yarn remove v1.3.2
warning package.json: No license field
[1/2] Removing module leftpad...
[2/2] Regenerating lockfile and installing missing dependencies...
warning No license field
success Uninstalled packages.
✨ Done in 0.13s.
您还可以在命令链中像使用其他命令一样使用 yarn:python setup.py yarn test sdist 等
关于Python setup.py 运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17887905/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我有一个在Linux服务器上运行的ruby脚本。它不使用rails或任何东西。它基本上是一个命令行ruby脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://