How to Return List of Packages from Pacman/Yaourt Search - linux

----EDIT----
Changed the name of the script from pacsearch to pacdot.
Apparently yaourt -Ssaq does this, so this script isn't as necessary as I thought. Although, I still find using pacdot -w to open the results in a text document helpful.
----/EDIT----
This isn't a question, but I thought someone else might find this useful. Someone may end up on stackoverflow trying to find a solution like this.
On Arch Linux, I keep finding myself searching with pacman or yaourt and wishing I could get just the package names, not all of the extra stuff. For example, I'd love to be able to run yaourt -Sa $(yaourt -Ssa package). Oddly enough, pacman and yaourt don't seem have this option (not that I can tell, at least), so I wrote a python script to do it. Copy it if you'd like. You can name it what you want, but I'll refer to it as pacdot.py.
pacdot.py package will be like yaourt -Ssa package but only list the package names.
I added a few extra options:
pacdot.py -o package will only list results from the official Arch repositories, not the AUR.
pacdot.py -i package will install all the found packages. If you've ever thought about running something like yaourt -Sa $(yaourt -Ssa package), that's what this command does.
pacdot.py -w package will:
Create a file called 'the-package-you-searched.txt',
Write an example command that would install the found packages,
(yaourt -Sa all-of-the-results),
Write each result on a new line, and
Open the file for you (with your default text editor).
Here's the code:
#!/bin/python3
import argparse
import re
from subprocess import Popen, PIPE, call
from collections import deque
desc = ''.join(('Search the official Arch and AUR databases ',
'and return package names only. ',
'e.g.: `pacdot.py arch` will return "arch", ',
'whereas `$ yaourt -Ssa arch` will return ',
'"community/arch 1.3.5-10',
' A modern and remarkable revision control system."'
))
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('package',
help='Package to search with pacman')
parser.add_argument('-o', '--official', action='store_true',
help='Search official repositories only, not the AUR')
parser.add_argument('-i', '--install', action='store_true',
help='Install found packages')
parser.add_argument('-w', '--write', action='store_true',
help='Write to file')
#Set args strings.
args = parser.parse_args()
pkg = args.package
official_only = args.official
install = args.install
write = args.write
# Do yaourt search.
package_search = Popen(['yaourt', '-Ssa', '%s' % pkg], stdout=PIPE).communicate()
# Put each found package into a list.
package_titles_descs = str(package_search[0]).split('\\n')
# Strip off the packages descriptions.
package_titles = [package_titles_descs[i]
for i in range(0, len(package_titles_descs), 2)]
# Remove empty item in list.
del(package_titles[-1])
# Make a separate list of the non-aur packages.
package_titles_official = deque(package_titles)
[package_titles_official.remove(p)
for p in package_titles if p.startswith('aur')]
# Strip off extra stuff like repository names and version numbers.
packages_all = [re.sub('([^/]+)/([^\s]+) (.*)',
r'\2', str(p))
for p in package_titles]
packages_official = [re.sub('([^/]+)/([^\s]+) (.*)',
r'\2', str(p))
for p in package_titles_official]
# Mark the aur packages.
# (Not needed, just in case you want to modify this script.)
#packages_aur = packages_all[len(packages_official):]
# Set target packages to 'all' or 'official repos only'
# based on argparse arguments.
if official_only:
packages = packages_official
else:
packages = packages_all
# Print the good stuff.
for p in packages:
print(p)
if write:
# Write results to file.
filename = ''.join((pkg, '.txt'))
with open(filename, 'a') as f:
print(''.join(('Yaourt search for "', pkg, '"\n')), file=f)
print('To install:', file=f)
packages_string = ' '.join(packages)
print(' '.join(('yaourt -Sa', packages_string)), file=f)
print('\nPackage list:', file=f)
for p in packages:
print(p, file=f)
# Open file.
call(('xdg-open', filename))
if install:
# Install packages with yaourt.
for p in packages:
print(''.join(('\n\033[1;32m==> ', '\033[1;37m', p,
'\033[0m')))
Popen(['yaourt', '-Sa', '%s' % p]).communicate()

You've just re-invented the wheel. pacman, packer and yaourt all have the -q flag.
For example:
yaourt -Ssq coreutils
Results:
coreutils
busybox-coreutils
coreutils-git
coreutils-icp
coreutils-selinux
coreutils-static
cv
cv-git
ecp
gnu2busybox-coreutils
gnu2plan9-coreutils
gnu2posix2001-coreutils
gnu2sysv-coreutils
gnu2ucb-coreutils
policycoreutils
selinux-usr-policycoreutils-old
smack-coreutils
xml-coreutils

Related

Alternative methods for deprecated gym.wrappers.Monitor()

The example is from Chapter 8 of Grokking Deep Reinforcement Learning written by Miguel Morales.
Please click here
The wrappers.Monitor is deprecated after the book is published. The code in question is as below:
env = wrappers.Monitor(
env, mdir, force=True,
mode=monitor_mode,
video_callable=lambda e_idx: record) if monitor_mode else env
I searched the internet and tried 2 methods but failed.
1- gnwrapper.Monitor
I pip install gym-notebook-wrapper and import gnwrapper, then rewrite the code
env = gnwrapper.Monitor(gym.make(env_name),directory="./")
A [WinError 2] The system cannot find the file specified error message is returned.
2- gym.wrappers.RecordVideo
I from gym.wrappers import RecordVideo then rewrite the code
env = RecordVideo(gym.make(env_name), "./")
A AttributeError: 'CartPoleEnv' object has no attribute 'videos' error message is returned.
Is there any way to solve the problem?
After some experiments, the best way to work around the problem is to downgrade the gym version to 0.20.0, in order to preserve the wrappers.Monitor function.
Moreover, the subprocess.Popen and subprocess.check_output does not work as the original author suggested, so I use moviepy (please pip install moviepy if you do not have this libarary and from moviepy.editor import VideoFileClip at the very beginning) to change MP4 to GIF files. The code in get_gif_html is amended as follows.
gif_path = basename + '.gif'
if not os.path.exists(gif_path):
videoClip = VideoFileClip(video_path)
videoClip.write_gif(gif_path, logger=None)
gif = io.open(gif_path, 'r+b').read()
The program now completely works.
Edit (24 Jul 2022):
The following solution is for gym version 0.25.0, as I hope the program works for later versions. Windows 10 and Juptyer Notebook is used for the demonstration.
(1) Maintain the moviepy improvement stated above
(2) import from gym.wrappers.record_video import RecordVideo and substitute
env = wrappers.Monitor(
env, mdir, force=True,
mode=monitor_mode,
video_callable=lambda e_idx: record) if monitor_mode else
with this
if monitor_mode:
env = RecordVideo(env, mdir, episode_trigger=lambda e_idx:record)
env.reset()
env.start_video_recorder()
else:
env = env
RecordVideo function takes (env, video_folder, episode_trigger) arguments, and video_callable and episode_trigger are the same thing.
(3) Loss of env.videos in get_gif_html function in new versions.
In the old setting, after the video is recorded, the MP4 and meta.json files are stored in the following address with a random file name
C:\Users\xxx\AppData\Local\Temp
https://i.stack.imgur.com/Za3Sd.jpg
This env.videos function returns a list of tuples, each tuples consisted of address path of MP4 and meta.json.
I tried to recreate the env.videos by
(a) declare the mdir as global variable
def get_make_env_fn(**kargs):
def make_env_fn(env_name, seed=None, render=None, record=False,
unwrapped=False, monitor_mode=None,
inner_wrappers=None, outer_wrappers=None):
global mdir
mdir = tempfile.mkdtemp()
(b) in the demo_progression and demo_last function, grouping all MP4 and meta_json files, zipping them and use list comprehension to make a new env_videos variable (as opposed to env.videos)
env.close()
mp4_files = ([(mdir + '\\' + file) for file in os.listdir(mdir) if file.endswith('.mp4')])
meta_json_files = ([(mdir + '\\' + file) for file in os.listdir(mdir) if file.endswith('.meta.json')])
env_videos = [(mp4, meta_json) for mp4, meta_json in zip(mp4_files, meta_json_files)]
data = get_gif_html(env_videos=env_videos,
These are all the changes.

Python list not sorting when called from inside a bash script

I am trying to print a custom help message for a bash script. Inside the script, I call a function that then uses python to parse the script, find the functions and the function help string, and then print them. This all works. But, when I try to sort the list of tuples that contains the function name and help string, the sort seems to be ignored. Similar code works as expected in a pure python environment.
Edit: Just noticed I tried the sort two different ways. AFAIK either should have sorted the list, but neither worked.
Edit again: see the accepted answer below for code that actually works. I need to remember to reread my problem code in the morning ;)
SCRIPT_PATH=$(realpath $0)
function build() { ## builds source and wheel package
echo "foo"
}
function aa() { ## foo
echo "foo"
}
function release() { ## package and upload a release
echo "foo"
}
function project:init:all() { ## Initialize a venv, update pip, wheels, and setuptools, and install both requirements files.
echo "foo"
}
function venv:init() { ## makes a venv in the project directory
echo "foo"
}
function print:help() {
echo $SCRIPT_PATH
python3 - << EOF
from pathlib import Path
from operator import itemgetter
import re
script_path = Path("$SCRIPT_PATH")
with open(script_path) as file:
for line in file:
match = re.match(r'^function\s*([a-zA-Z0-9\:-]*)\(\)\s*{\s*##\s*(.*)', line)
matches = []
if match is not None:
target, help = match.groups()
matches.append((target,help))
#for help_line in sorted(matches,key=lambda func: func[1]):
matches.sort(key=itemgetter(1))
for help_line in matches:
print(" {0:20} {1}".format(target,help))
EOF
}
results in:
build builds source and wheel package
aa foo
release package and upload a release
project:init:all Initialize a venv, update pip, wheels, and setuptools, and install both requirements files.
venv:init makes a venv in the project directory
but I expected:
aa foo
build builds source and wheel package
project:init:all Initialize a venv, update pip, wheels, and setuptools, and install both requirements files.
release package and upload a release
venv:init makes a venv in the project directory
You need to get all the functions, then sort and print :
function print:help() {
echo $SCRIPT_PATH
python3 - << EOF
from pathlib import Path
from operator import itemgetter
import re
script_path = Path("$SCRIPT_PATH")
with open(script_path) as file:
functions = []
for line in file:
match = re.match(r'^function\s*([a-zA-Z0-9\:-]*)\(\)\s*{\s*##\s*(.*)', line)
if match is not None:
functions.append(match.groups())
for target, help in sorted(functions):
print(" {0:20} {1}".format(target,help))
EOF
}

How to get the default application mapped to a file extention in windows using Python

I would like to query Windows using a file extension as a parameter (e.g. ".jpg") and be returned the path of whatever app windows has configured as the default application for this file type.
Ideally the solution would look something like this:
from stackoverflow import get_default_windows_app
default_app = get_default_windows_app(".jpg")
print(default_app)
"c:\path\to\default\application\application.exe"
I have been investigating the winreg builtin library which holds the registry infomation for windows but I'm having trouble understanding its structure and the documentation is quite complex.
I'm running Windows 10 and Python 3.6.
Does anyone have any ideas to help?
The registry isn't a simple well-structured database. The Windows
shell executor has some pretty complex logic to it. But for the simple cases, this should do the trick:
import shlex
import winreg
def get_default_windows_app(suffix):
class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, suffix)
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
command = winreg.QueryValueEx(key, '')[0]
return shlex.split(command)[0]
>>> get_default_windows_app('.pptx')
'C:\\Program Files\\Microsoft Office 15\\Root\\Office15\\POWERPNT.EXE'
Though some error handling should definitely be added too.
Added some improvements to the nice code by Hetzroni, in order to handle more cases:
import os
import shlex
import winreg
def get_default_windows_app(ext):
try: # UserChoice\ProgId lookup initial
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{}\UserChoice'.format(ext)) as key:
progid = winreg.QueryValueEx(key, 'ProgId')[0]
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SOFTWARE\Classes\{}\shell\open\command'.format(progid)) as key:
path = winreg.QueryValueEx(key, '')[0]
except: # UserChoice\ProgId not found
try:
class_root = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, ext)
if not class_root: # No reference from ext
class_root = ext # Try direct lookup from ext
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r'{}\shell\open\command'.format(class_root)) as key:
path = winreg.QueryValueEx(key, '')[0]
except: # Ext not found
path = None
# Path clean up, if any
if path: # Path found
path = os.path.expandvars(path) # Expand env vars, e.g. %SystemRoot% for ext .txt
path = shlex.split(path, posix=False)[0] # posix False for Windows operation
path = path.strip('"') # Strip quotes
# Return
return path

How to configure Yocto for an example "hello world" program which uses a Debian library?

I build a simple software in my Debian VM. It tries the download a file from a FTP server. For that I use the libcurl library(libcurl-openssl-dev).
Now I have a SAMA5D3x-Evalboard available. For self education purpose I like to build a Linux + Toolchain for it. I found the Yocto project. The meta layer concept seems to be good approach for managing different platforms - so let's try it!
I found some examples at the net. But no which covers the combination of "hello world" + bigger lib.
I want:
get the X-compile toolchain + Linux image(includes my hello world + the libcurl shared objects)
My approach:
meta-atmel: for the SAMA5D3x eval board
meta-debian: for the lib "libcurl-openssl-dev"
meta-mySoftware: for the hello world program
The constrains are:
main.c - name of the hello world program
libcurl - c lib name (I have chosen this lib, because it has a lot of dependencies)
How to setup it right? If there are better ideas they are welcome!
Installation & Configuration of Yocto
I.) install toolchain
I.1.) apt-get:
sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm
I.2.) opt. configure git after proxy
git config --global core.gitproxy <gitproxy>
I.3.) download Toolchain + meta packages and checkout the "right" commit
git clone git://git.yoctoproject.org/poky
cd poky
git checkout jethro
git clone git://github.com/linux4sam/meta-atmel.git
cd meta-atmel
git checkout jethro
cd ..
git clone git://git.openembedded.org/meta-openembedded
cd meta-openembedded
git checkout jethro
cd ..
II.) build kernel+rootfs for SAMA5D3x
II.1) create new project
source oe-init-build-env
the new project is created at the "build" dir
modify layers "conf/bblayers.conf" via commandos
bitbake-layers add-layer "../meta-atmel"
bitbake-layers add-layer "../meta-openembedded/meta-oe"
bitbake-layers add-layer "../meta-openembedded/meta-networking"
bitbake-layers add-layer "../meta-openembedded/meta-python"
edit of "conf/local.conf"
MACHINE ??= "sama5d3xek"
DL_DIR ?= "${TOPDIR}/../downloads"
DISTRO ?= "poky-atmel"
the download directory is outside of the build environment. The downloaded sources can be used for different builds.
II.2) create image
bitbake -c clean core-image-minimal
bitbake core-image-minimal
the result files are created at "tmp/deploy/images/sama5d3xek"
II.3) flash image
The sam-ba tool from Atmel is needed. The Flash-Process can be automated by a script(we create a tcl file). The self written tool "buildAtmelImages.py" copies the needed image files for the SAMA5D35, creates the u-boot configuration and a Flash-Script. An Example:
python buildAtmelImages.py --help
python buildAtmelImages.py -s "/home/user/poky/build/tmp/deploy/images/sama5d3xek" -d "/home/user/images" -t "\\\\1.2.3.4\\admin\\images"
here comes the code for "buildAtmelImages.py":
#!/usr/bin/python
# encoding: utf-8
'''
buildAtmelImages -- make yocto build images ready of the sam-ba flash tool
buildAtmelImages is a description
- reverse engineered /meta-atmel/scripts/nandflash.tcl script
- works only for the SAMA5D35
- params:
-s "<dir>" = source dir of the images generated by yocto. like "/home/user/poky/build/tmp/deploy/images/sama5d3xek"
-d "<dir>" = destination dir
-t "<dir/url>" = tcl refence dir. The path to the images, used to flash, is hard coded in the tcl script. typical example "\\\\192.168.1.2\\admin\\images" for an samba based network
- way of operation:
# hints:
I.) the SAMA5D3x implements some ECC features. This means that in every NAND sector are bits for error correction reserved.
This acts into 2 directions:
1.) the binary files have to be flashed by takeing care of the extra ECC bits. This is done by the sam-ba flash tool
2.) the kernel needs to take care, that the data stored on the NAND drive has some extra handling. This is managed by the meta-atmel package used by the yocto toolchain.
II.) u boot configuration
1.) the u boot configuration is placed into the NAND. But the configuration is not generated by the yocto toolchain. Atmel delivers a flashing script for its demo boards. This was reversed engineered into this python script.
The layout of the boot config is a defined memory space which is guared by an crc32(litle endian). The boot configuration is placed as ANSII string parameters. These parameters are seperated by a zero.
2.) because of the ecc the binary device tree(dtb) and kernel needs to be copied to memory before been executed. For that it is needed that u-boot loads them into ram before starting the kernel.
# operation:
First the generated images are copied. After the configuartion is generated and placed into the output dir. At least the tcl script is generated and placed into the output dir, as well.
#author: Stefan Jaritz
#copyright:
#license: license
#contact:
#deffield updated: Updated
'''
import sys
import os
import shutil
import binascii
import struct
from argparse import ArgumentParser
from argparse import RawDescriptionHelpFormatter
__all__ = []
__version__ = 0.1
__date__ = '2016-04-19'
__updated__ = '2016-04-19'
class _memorySeg():
def __init__(self, add, sf, df):
self.add = add
self.sf = sf
self.df = df
gArch = 'sama5d3xek'
gTCLfilename = 'SEK4.tcl'
gMemSetup = {
'bootloader' : _memorySeg(0x00000000, 'at91bootstrap-sama5d3xek.bin', 'bootloader.bin'),
'uboot' : _memorySeg(0x00040000, 'u-boot-sama5d3xek.bin', 'uboot.bin'),
'ubootEnv' : _memorySeg(0x000c0000, None, 'ubootEnv.bin'),
'ubootEnvRed' : _memorySeg(0x00100000, None, 'ubootEnv.bin'),
'dtb' : _memorySeg(0x00180000,'zImage-sama5d35ek.dtb', 'kernel.dtb'),
'kernel' : _memorySeg(0x00200000,'zImage', 'kernel'),
'rootfs' : _memorySeg(0x00800000,'core-image-minimal-sama5d3xek.ubi', 'rootfs.ubi')
}
gSourceDir = ''
gDestDir = ''
gTCLfilepath = ''
gECCcnfg = {
'at91sam9x5ek' : 0xc0c00405,
'at91sam9n12ek' : 0xc0c00405,
'sama5d3xek' :0xc0902405,
'sama5d3_xplained' : 0xc0902405,
'sama5d4ek' : 0xc1e04e07,
'sama5d4_xplained' : 0xc1e04e07
}
def _copyFiles ():
''' copies image files from source to the destination'''
global gMemSetup
for (k, ms) in gMemSetup.items():
if ms.sf is not None:
print("copy {k}".format(k=k))
shutil.copy(os.path.join(gSourceDir, ms.sf), os.path.join(gDestDir, ms.df))
def _genUBootCfg ():
global gMemSetup
global gSourceDir
global gDestDir
print("generate u-boot config")
bootCfgStr = "bootcmd=nand read 0x{dtbLoadAddr:X} 0x{dtbAddr:X} {dtbSize}; nand read 0x{kernelLoadAddr:X} 0x{kernelAddr:X} {kernelSize}; bootz 0x{kernelLoadAddr:X} - 0x{dtbLoadAddr:X}".format(
dtbLoadAddr=0x21000000,
dtbAddr=gMemSetup['dtb'].add,
dtbSize=os.path.getsize(os.path.join(gSourceDir, gMemSetup['dtb'].sf)),
kernelLoadAddr=0x22000000,
kernelAddr=gMemSetup['kernel'].add,
kernelSize=os.path.getsize(os.path.join(gSourceDir, gMemSetup['kernel'].sf))
)
bootVars = (
'bootdelay=0',
'baudrate=115200',
'stdin=serial',
'stdout=serial',
'stderr=serial',
'bootargs=console=ttyS0,115200 mtdparts=atmel_nand:256k(bootstrap)ro,512k(uboot)ro,256k(env),256k(env_redundant),256k(spare),512k(dtb),6M(kernel)ro,-(rootfs) rootfstype=ubifs ubi.mtd=7 root=ubi0:rootfs rw',
bootCfgStr
)
cfgMemCfg = bytearray(0x20000-5)
l = len(cfgMemCfg)
indx = 0
for v in bootVars:
l = len(v)
cfgMemCfg[indx:indx+l] = v.encode('utf-8')
indx += l + 1
crc = binascii.crc32(cfgMemCfg)
with open(os.path.join(gDestDir, gMemSetup['ubootEnv'].df), mode='wb') as f:
# crc32 as little endian
f.write(struct.pack('<I',crc))
f.write(bytearray(1))
f.write(cfgMemCfg)
f.close()
def _helper_genTCLFilename (idName):
pstr = os.path.join(gTCLfilepath ,gMemSetup[idName].df)
pstr = pstr.replace('/', '\\')
return pstr.replace('\\', '\\\\')
def _genFlashScript():
global gMemSetup
global gECCcnfg
global gTCLfilename
global gDestDir
global gArch
print("generate tcl script for sam-ba")
tclStr = """puts "-I- start flashing"
NANDFLASH::Init
NANDFLASH::NandHeaderValue HEADER 0x{pmeccConfig:X}
NANDFLASH::EraseAllNandFlash
NANDFLASH::SendBootFilePmeccCmd "{bootstrapFile}"
send_file {{NandFlash}} "{ubootFile}" 0x{ubootAddr:X} 0
send_file {{NandFlash}} "{ubootEnvFile}" 0x{ubootEnvAddr:X} 0
send_file {{NandFlash}} "{ubootEnvFileRed}" 0x{ubootEnvAddrRed:X} 0
send_file {{NandFlash}} "{dtbFile}" 0x{dtbAddr:X} 0
send_file {{NandFlash}} "{kernelFile}" 0x{kernelAddr:X} 0
NANDFLASH::NandSetTrimffs 1
send_file {{NandFlash}} "{rootfsFile}" 0x{rootfsAddr:X} 0
puts "-I- finished flashing"
""".format(
pmeccConfig=gECCcnfg[gArch],
bootstrapFile=_helper_genTCLFilename('bootloader'),
ubootFile=_helper_genTCLFilename('uboot'),
ubootAddr=gMemSetup['uboot'].add,
ubootEnvFile=_helper_genTCLFilename('ubootEnv'),
ubootEnvAddr=gMemSetup['ubootEnv'].add,
ubootEnvFileRed=_helper_genTCLFilename('ubootEnvRed'),
ubootEnvAddrRed=gMemSetup['ubootEnvRed'].add,
dtbFile=_helper_genTCLFilename('dtb'),
dtbAddr=gMemSetup['dtb'].add,
kernelFile=_helper_genTCLFilename('kernel'),
kernelAddr=gMemSetup['kernel'].add,
rootfsFile=_helper_genTCLFilename('rootfs'),
rootfsAddr=gMemSetup['rootfs'].add
)
with open(os.path.join(gDestDir, gTCLfilename), mode='w') as f:
f.write(tclStr)
f.close()
class _CLIError(Exception):
'''Generic exception to raise and log different fatal errors.'''
def __init__(self, msg):
super(_CLIError).__init__(type(self))
self.msg = "E: %s" % msg
def __str__(self):
return self.msg
def __unicode__(self):
return self.msg
def main(argv=None): # IGNORE:C0111
'''Command line options.'''
global gSourceDir
global gDestDir
global gTCLfilepath
if argv is None:
argv = sys.argv
else:
sys.argv.extend(argv)
program_name = os.path.basename(sys.argv[0])
program_version = "v%s" % __version__
program_build_date = str(__updated__)
program_version_message = '%%(prog)s %s (%s)' % (program_version, program_build_date)
program_shortdesc = __import__('__main__').__doc__.split("\n")[1]
program_license = '''%s
Created by Stefan Jaritz on %s.
Copyright 2016 organization_name. All rights reserved.
Licensed under the Apache License 2.0
http://www.apache.org/licenses/LICENSE-2.0
Distributed on an "AS IS" basis without warranties
or conditions of any kind, either express or implied.
USAGE
''' % (program_shortdesc, str(__date__))
try:
# Setup argument parser
parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter)
parser.add_argument("-s", "--source", dest="srcD", help="source path of the images [default: %(default)s]", default=os.getcwd())
parser.add_argument("-d", "--destination", dest="dstD", help="destination where the images and the flash script is copied [default: %(default)s]", default=os.getcwd() )
parser.add_argument("-t", "--tclFilePath", dest="tclD", help="TCL script path. The path where the TCL file gona be located [default: %(default)s]", default='' )
parser.add_argument('-V', '--version', action='version', version=program_version_message)
# Process arguments
args = parser.parse_args()
gSourceDir = args.srcD
gDestDir = args.dstD
gTCLfilepath = args.tclD
shutil.rmtree(gDestDir, ignore_errors=True)
os.makedirs(gDestDir)
print("start")
_copyFiles()
_genUBootCfg()
_genFlashScript()
print("finished")
return 0
except KeyboardInterrupt:
### handle keyboard interrupt ###
return -1
except Exception as e:
indent = len(program_name) * " "
sys.stderr.write(program_name + ": " + repr(e) + "\n")
sys.stderr.write(indent + " for help use --help")
return -2
if __name__ == "__main__":
sys.exit(main())
III.) Create own package
yocto-layer create own
bitbake-layers add-layer "../meta-own"
modify the example.bb
SRC_URI = "file://testFTP.c"
S = "${WORKDIR}"
DEPENDS ="curl"
do_compile() {
${CC} testFTP.c -o testFTP ${CFLAGS} -lcurl
}
do_install() {
install -d ${D}${bindir}
install -m 0755 testFTP ${D}${bindir}
}
now add in "layer.conf"
IMAGE_INSTALL_append = " vim testftp"
Now the vim editor and the testftp recipe, which includes the testFTP binary is installed at the "user/bin" at the rootfs.
After the flashing, start the the SAMA5D35 Evalboard. Login as "root". Type: "testFTP" or "vim" and enjoy the output.
Cheers!

How to create a symbolic link with SCons?

I'm using SCons for building a project and need to add a symbolic link to a file it is installing via env.Install. What command(s) will make a link that's the equivalent of running ln -s on the command line?
SCons doesn't have a dedicated symbolic link command, but you can use os.symlink(src, dst) from Python's os module:
import os
env = Environment()
def SymLink(target, source, env):
os.symlink(os.path.abspath(str(source[0])), os.path.abspath(str(target[0])))
env.Command("file.out", "file.in", SymLink)
This may not work correctly on Windows, I've only tried it on Linux.
There seems to be little advancement in the SCons core code for symbolic link support and I wasn't satisfied any one solution I found on the web. Here is a potential builder which incorporates aspects of both Nick's and richq's answers. Additionally, it will catch name changes (due to the emitter method) and is as platform-agnostic as I could get it.
I prefer this builder because it will make links relative to the directory in which they are installed. One could add an option to force the link to be absolute I suppose, but I have not needed or wanted that yet.
Currently, if the OS doesn't support symlinks, I just pass and do nothing, but one could use os.copytree() for example however the dependency becomes messy if the source is a directory so the emitter would need to do something fancy. I'm up for any suggestions here.
One can put the following code into the file site_scons/site_tools/symlink.py (with blank _init_.py files in the appropriate places). Then do this in the SConstruct file:
SConstruct:
env = Environment()
env.Tool('symlink')
env.SymLink('link_name.txt', 'real_file.txt')
symlink.py:
import os
from os import path
from SCons.Node import FS
from SCons.Script import Action, Builder
def generate(env):
'''
SymLink(link_name,source)
env.SymLink(link_name,source)
Makes a symbolic link named "link_name" that points to the
real file or directory "source". The link produced is always
relative.
'''
bldr = Builder(action = Action(symlink_builder,symlink_print),
target_factory = FS.File,
source_factory = FS.Entry,
single_target = True,
single_source = True,
emitter = symlink_emitter)
env.Append(BUILDERS = {'SymLink' : bldr})
def exists(env):
'''
we could test if the OS supports symlinks here, or we could
use copytree as an alternative in the builder.
'''
return True
def symlink_print(target, source, env):
lnk = path.basename(target[0].abspath)
src = path.basename(source[0].abspath)
return 'Link: '+lnk+' points to '+src
def symlink_emitter(target, source, env):
'''
This emitter removes the link if the source file name has changed
since scons does not seem to catch this case.
'''
lnk = target[0].abspath
src = source[0].abspath
lnkdir,lnkname = path.split(lnk)
srcrel = path.relpath(src,lnkdir)
if int(env.get('verbose',0)) > 3:
ldir = path.relpath(lnkdir,env.Dir('#').abspath)
if rellnkdir[:2] == '..':
ldir = path.abspath(ldir)
print ' symbolic link in directory: %s' % ldir
print ' %s -> %s' % (lnkname,srcrel)
try:
if path.exists(lnk):
if os.readlink(lnk) != srcrel:
os.remove(lnk)
except AttributeError:
# no symlink available, so we remove the whole tree? (or pass)
#os.rmtree(lnk)
print 'no os.symlink capability on this system?'
return (target, source)
def symlink_builder(target, source, env):
lnk = target[0].abspath
src = source[0].abspath
lnkdir,lnkname = path.split(lnk)
srcrel = path.relpath(src,lnkdir)
if int(env.get('verbose',0)) > 4:
print 'target:', target
print 'source:', source
print 'lnk:', lnk
print 'src:', src
print 'lnkdir,lnkname:', lnkdir, lnkname
print 'srcrel:', srcrel
if int(env.get('verbose',0)) > 4:
print 'in directory: %s' % path.relpath(lnkdir,env.Dir('#').abspath)
print ' symlink: %s -> %s' % (lnkname,srcrel)
try:
os.symlink(srcrel,lnk)
except AttributeError:
# no symlink available, so we make a (deep) copy? (or pass)
#os.copytree(srcrel,lnk)
print 'no os.symlink capability on this system?'
return None
This creates a builder to perform the job:
mylib = env.SharedLibrary("foobar", SRCS)
builder = Builder(action = "ln -s ${SOURCE.file} ${TARGET.file}", chdir = True)
env.Append(BUILDERS = {"Symlink" : builder})
mylib_link = env.Symlink("_foobar.so", mylib)
env.Default(mylib)
env.Default(mylib_link)
Again, this solution is for Linux.
If you wanted to issue the command directly to the shell and know the OS, subprocess can be used as well.
E.g.: subprocess.call(['ln', '-s', '</src/path>', '</dest/path>'])
In addition to Nicks solution, you can add a directory symlink by using a file as a directory name carrier. It's not the cleanest solution and debugging path names is a pain, but this works well:
def symlink_last(target_source_env):
src = os.path.basename(os.path.dirname(str(source[0])))
link = "deliverables/last"
print "Symlinking "+ src + "as" + link
os.symlink(src, link)
BUILD_TARGETS.append('link')
install_dir = "deliverables/subdir"
carrier_file = "filename"
builder = Builder(action = symlink_last, chdir=False)
env.Append(BUILDERS={ "Symlink" : builder })
env.Alias(target="link", source=env.Symlink(dir="deliverables", source = install_dir + carrier_file)
This will make a link to deliverables/subdir named deliverables/last, provided that a file deliverables/subdir/filename exists.

Resources