pax_global_header00006660000000000000000000000064130045356410014513gustar00rootroot0000000000000052 comment=f073f057d3fd2148866eaa0444a62ababfdf935e .gitignore000066400000000000000000000000071300453564100130440ustar00rootroot00000000000000build/ CMakeLists.txt000066400000000000000000000004551300453564100136230ustar00rootroot00000000000000CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR) CMAKE_POLICY(VERSION 2.6) FIND_PACKAGE(Torch REQUIRED) SET(src sys.c) SET(luasrc init.lua colors.lua fpath.lua) ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}") TARGET_LINK_LIBRARIES(sys luaT TH) IF(LUALIB) TARGET_LINK_LIBRARIES(sys ${LUALIB}) ENDIF() LICENSE000066400000000000000000000032251300453564100120660ustar00rootroot00000000000000Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) Copyright (c) 2011-2013 NYU (Clement Farabet) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the names of NEC Laboratories American and IDIAP Research Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. README.md000066400000000000000000000024621300453564100123420ustar00rootroot00000000000000# Lua *system* package Note: some functions only work on UNIX systems. ## Dependencies Torch7 (www.torch.ch) ## Install ``` $ luarocks install sys ``` ## Use ```lua $ torch > require 'sys' ``` ### Time / Clock ```lua > t = sys.clock() -- high precision clock (us precision) > sys.tic() > -- do something > t = sys.toc() -- high precision tic/toc > sys.sleep(1.5) -- sleep 1.5 seconds ``` ### Paths ```lua > path,fname = sys.fpath() ``` Always returns the path of the file in which this call is made. Useful to access local resources (non-lua files). ### Execute By default, Lua's `os.execute` doesn't pipe its results (stdout). This function uses popen to pipe its results into a Lua string: ```lua > res = sys.execute('ls -l') > print(res) ``` Derived from this, a few commands: ```lua > print(sys.uname()) linux ``` UNIX-only: shortcuts to run bash commands: ```lua > ls() > ll() > lla() ``` ### sys.COLORS If you'd like print in colours, follow the following snippets of code. Let start by listing the available colours ```lua $ torch > for k in pairs(sys.COLORS) do print(k) end ``` Then, we can generate a shortcut `c = sys.COLORS` and use it within a `print` ```lua > c = sys.COLORS > print(c.magenta .. 'This ' .. c.red .. 'is ' .. c.yellow .. 'a ' .. c.green .. 'rainbow' .. c.cyan .. '!') ``` colors.lua000066400000000000000000000014741300453564100130710ustar00rootroot00000000000000-------------------------------------------------------------------------------- -- colors, can be used to print things in color -------------------------------------------------------------------------------- local colors = { none = '\27[0m', black = '\27[0;30m', red = '\27[0;31m', green = '\27[0;32m', yellow = '\27[0;33m', blue = '\27[0;34m', magenta = '\27[0;35m', cyan = '\27[0;36m', white = '\27[0;37m', Black = '\27[1;30m', Red = '\27[1;31m', Green = '\27[1;32m', Yellow = '\27[1;33m', Blue = '\27[1;34m', Magenta = '\27[1;35m', Cyan = '\27[1;36m', White = '\27[1;37m', _black = '\27[40m', _red = '\27[41m', _green = '\27[42m', _yellow = '\27[43m', _blue = '\27[44m', _magenta = '\27[45m', _cyan = '\27[46m', _white = '\27[47m' } return colors fpath.lua000066400000000000000000000006641300453564100126720ustar00rootroot00000000000000-------------------------------------------------------------------------------- -- always returns the path of the file running -------------------------------------------------------------------------------- local function fpath() local fpath = _G.debug.getinfo(2).source:gsub('@','') if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end return paths.dirname(fpath),paths.basename(fpath) end return fpath init.lua000066400000000000000000000131161300453564100125270ustar00rootroot00000000000000---------------------------------------------------------------------- -- sys - a package that provides simple system (unix) tools ---------------------------------------------------------------------- local os = require 'os' local io = require 'io' local paths = require 'paths' sys = {} -------------------------------------------------------------------------------- -- load all functions from lib -------------------------------------------------------------------------------- local _lib = require 'libsys' for k,v in pairs(_lib) do sys[k] = v end -------------------------------------------------------------------------------- -- tic/toc (matlab-like) timers -------------------------------------------------------------------------------- local __t__ function sys.tic() __t__ = sys.clock() end function sys.toc(verbose) local __dt__ = sys.clock() - __t__ if verbose then print(__dt__) end return __dt__ end -------------------------------------------------------------------------------- -- execute an OS command, but retrieves the result in a string -------------------------------------------------------------------------------- local function execute(cmd) local cmd = cmd .. ' 2>&1' local f = io.popen(cmd) local s = f:read('*all') f:close() s = s:gsub('^%s*',''):gsub('%s*$','') return s end sys.execute = execute -------------------------------------------------------------------------------- -- execute an OS command, but retrieves the result in a string -- side effect: file in /tmp -- this call is typically more robust than the one above (on some systems) -------------------------------------------------------------------------------- function sys.fexecute(cmd, readwhat) readwhat = readwhat or '*all' local tmpfile = os.tmpname() local cmd = cmd .. ' >'.. tmpfile..' 2>&1' os.execute(cmd) local file = _G.assert(io.open(tmpfile)) local s= file:read(readwhat) file:close() s = s:gsub('^%s*',''):gsub('%s*$','') os.remove(tmpfile) return s end -------------------------------------------------------------------------------- -- returns the name of the OS in use -- warning, this method is extremely dumb, and should be replaced by something -- more reliable -------------------------------------------------------------------------------- function sys.uname() if paths.dirp('C:\\') then return 'windows' else local ffi = require 'ffi' local os = ffi.os if os:find('Linux') then return 'linux' elseif os:find('OSX') then return 'macos' else return '?' end end end sys.OS = sys.uname() -------------------------------------------------------------------------------- -- ls (list dir) -------------------------------------------------------------------------------- sys.ls = function(d) d = d or ' ' return execute('ls ' ..d) end sys.ll = function(d) d = d or ' ' return execute('ls -l ' ..d) end sys.la = function(d) d = d or ' ' return execute('ls -a ' ..d) end sys.lla = function(d) d = d or ' ' return execute('ls -la '..d) end -------------------------------------------------------------------------------- -- prefix -------------------------------------------------------------------------------- local function find_prefix() if arg then for i, v in pairs(arg) do if type(i) == "number" and type(v) == "string" and i <= 0 then local lua_path = paths.basename(v) if lua_path == "luajit" or lua_path == "lua" then local bin_dir = paths.dirname(v) if paths.basename(bin_dir) == "bin" then return paths.dirname(bin_dir) else return bin_dir end end end end end return "" end sys.prefix = find_prefix() -------------------------------------------------------------------------------- -- always returns the path of the file running -------------------------------------------------------------------------------- sys.fpath = require 'sys.fpath' -------------------------------------------------------------------------------- -- split string based on pattern pat -------------------------------------------------------------------------------- function sys.split(str, pat) local t = {} -- NOTE: use {n = 0} in Lua-5.0 local fpat = "(.-)" .. pat local last_end = 1 local s, e, cap = str:find(fpat, last_end) while s do if s ~= 1 or cap ~= "" then _G.table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) _G.table.insert(t, cap) end return t end -------------------------------------------------------------------------------- -- check if a number is NaN -------------------------------------------------------------------------------- function sys.isNaN(number) -- We rely on the property that NaN is the only value that doesn't equal itself. return (number ~= number) end -------------------------------------------------------------------------------- -- sleep -------------------------------------------------------------------------------- function sys.sleep(seconds) sys.usleep(seconds*1000000) end -------------------------------------------------------------------------------- -- colors, can be used to print things in color -------------------------------------------------------------------------------- sys.COLORS = require 'sys.colors' -------------------------------------------------------------------------------- -- backward compat -------------------------------------------------------------------------------- sys.dirname = paths.dirname sys.concat = paths.concat return sys sys-1.0-0.rockspec000066400000000000000000000011441300453564100140610ustar00rootroot00000000000000package = "sys" version = "1.0-0" source = { url = "git://github.com/torch/sys", tag = "1.0-0" } description = { summary = "A system library for Torch", detailed = [[ Provides system functionalities for Torch. ]], homepage = "https://github.com/torch/sys", license = "BSD" } dependencies = { "torch >= 7.0", } build = { type = "command", build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], install_command = "cd build && $(MAKE) install" } sys-1.1-0.rockspec000066400000000000000000000011131300453564100140560ustar00rootroot00000000000000package = "sys" version = "1.1-0" source = { url = "git://github.com/torch/sys" } description = { summary = "A system library for Torch", detailed = [[ Provides system functionalities for Torch. ]], homepage = "https://github.com/torch/sys", license = "BSD" } dependencies = { "torch >= 7.0", "luaffi" } build = { type = "command", build_command = [[cmake -E make_directory build && cd build && cmake .. -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], install_command = "cd build && $(MAKE) install" } sys.c000066400000000000000000000032001300453564100120340ustar00rootroot00000000000000#include #include #ifdef _WIN32 #define WINDOWS_LEAN_AND_MEAN #include #include static int l_clock(lua_State *L) { static const uint64_t EPOCH = 116444736000000000ULL; SYSTEMTIME systemtime; FILETIME filetime; uint64_t time; GetSystemTime(&systemtime); GetSystemTimeAsFileTime(&filetime); time = (((uint64_t)filetime.dwHighDateTime) << 32) + ((uint64_t)filetime.dwLowDateTime); double precise_time = (time - EPOCH) / 10000000.0; lua_pushnumber(L, precise_time); return 1; } static int l_usleep(lua_State *L) { int time = 1; if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1); Sleep(time / 1000); return 1; } #else #include #include #include #include #include #include #include static int l_clock(lua_State *L) { struct timeval tv; struct timezone tz; struct tm *tm; gettimeofday(&tv, &tz); tm=localtime(&tv.tv_sec); double precise_time = tv.tv_sec + tv.tv_usec / 1e6; lua_pushnumber(L,precise_time); return 1; } static int l_usleep(lua_State *L) { int time = 1; if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1); usleep(time); return 1; } #endif static const struct luaL_Reg routines [] = { {"clock", l_clock}, {"usleep", l_usleep}, {NULL, NULL} }; #if defined(_WIN32) #define SYS_DLLEXPORT __declspec(dllexport) __cdecl #else #define SYS_DLLEXPORT #endif int SYS_DLLEXPORT luaopen_libsys(lua_State *L) { lua_newtable(L); #if LUA_VERSION_NUM == 501 luaL_register(L, NULL, routines); #else luaL_setfuncs(L, routines, 0); #endif return 1; }