├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── colors.lua ├── fpath.lua ├── init.lua ├── sys-1.0-0.rockspec ├── sys-1.1-0.rockspec └── sys.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR) 2 | CMAKE_POLICY(VERSION 2.6) 3 | 4 | FIND_PACKAGE(Torch REQUIRED) 5 | 6 | SET(src sys.c) 7 | SET(luasrc init.lua colors.lua fpath.lua) 8 | 9 | ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}") 10 | 11 | TARGET_LINK_LIBRARIES(sys luaT TH) 12 | IF(LUALIB) 13 | TARGET_LINK_LIBRARIES(sys ${LUALIB}) 14 | ENDIF() 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) 2 | Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) 3 | Copyright (c) 2011-2013 NYU (Clement Farabet) 4 | 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in the 15 | documentation and/or other materials provided with the distribution. 16 | 17 | 3. Neither the names of NEC Laboratories American and IDIAP Research 18 | Institute nor the names of its contributors may be used to endorse or 19 | promote products derived from this software without specific prior 20 | written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lua *system* package 2 | 3 | Note: some functions only work on UNIX systems. 4 | 5 | ## Dependencies 6 | Torch7 (www.torch.ch) 7 | 8 | ## Install 9 | ``` 10 | $ luarocks install sys 11 | ``` 12 | 13 | ## Use 14 | 15 | ```lua 16 | $ torch 17 | > require 'sys' 18 | ``` 19 | 20 | ### Time / Clock 21 | 22 | ```lua 23 | > t = sys.clock() -- high precision clock (us precision) 24 | > sys.tic() 25 | > -- do something 26 | > t = sys.toc() -- high precision tic/toc 27 | > sys.sleep(1.5) -- sleep 1.5 seconds 28 | ``` 29 | 30 | ### Paths 31 | 32 | ```lua 33 | > path,fname = sys.fpath() 34 | ``` 35 | 36 | Always returns the path of the file in which this call is made. Useful 37 | to access local resources (non-lua files). 38 | 39 | ### Execute 40 | 41 | By default, Lua's `os.execute` doesn't pipe its results (stdout). This 42 | function uses popen to pipe its results into a Lua string: 43 | 44 | ```lua 45 | > res = sys.execute('ls -l') 46 | > print(res) 47 | ``` 48 | 49 | Derived from this, a few commands: 50 | 51 | ```lua 52 | > print(sys.uname()) 53 | linux 54 | ``` 55 | 56 | UNIX-only: shortcuts to run bash commands: 57 | 58 | ```lua 59 | > ls() 60 | > ll() 61 | > lla() 62 | ``` 63 | 64 | ### sys.COLORS 65 | 66 | If you'd like print in colours, follow the following snippets of code. Let start by listing the available colours 67 | 68 | ```lua 69 | $ torch 70 | > for k in pairs(sys.COLORS) do print(k) end 71 | ``` 72 | 73 | Then, we can generate a shortcut `c = sys.COLORS` and use it within a `print` 74 | 75 | ```lua 76 | > c = sys.COLORS 77 | > print(c.magenta .. 'This ' .. c.red .. 'is ' .. c.yellow .. 'a ' .. c.green .. 'rainbow' .. c.cyan .. '!') 78 | ``` 79 | -------------------------------------------------------------------------------- /colors.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- colors, can be used to print things in color 3 | -------------------------------------------------------------------------------- 4 | local colors = { 5 | none = '\27[0m', 6 | black = '\27[0;30m', 7 | red = '\27[0;31m', 8 | green = '\27[0;32m', 9 | yellow = '\27[0;33m', 10 | blue = '\27[0;34m', 11 | magenta = '\27[0;35m', 12 | cyan = '\27[0;36m', 13 | white = '\27[0;37m', 14 | Black = '\27[1;30m', 15 | Red = '\27[1;31m', 16 | Green = '\27[1;32m', 17 | Yellow = '\27[1;33m', 18 | Blue = '\27[1;34m', 19 | Magenta = '\27[1;35m', 20 | Cyan = '\27[1;36m', 21 | White = '\27[1;37m', 22 | _black = '\27[40m', 23 | _red = '\27[41m', 24 | _green = '\27[42m', 25 | _yellow = '\27[43m', 26 | _blue = '\27[44m', 27 | _magenta = '\27[45m', 28 | _cyan = '\27[46m', 29 | _white = '\27[47m' 30 | } 31 | 32 | return colors 33 | -------------------------------------------------------------------------------- /fpath.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- always returns the path of the file running 3 | -------------------------------------------------------------------------------- 4 | local function fpath() 5 | local fpath = _G.debug.getinfo(2).source:gsub('@','') 6 | if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end 7 | return paths.dirname(fpath),paths.basename(fpath) 8 | end 9 | 10 | return fpath 11 | -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------- 2 | -- sys - a package that provides simple system (unix) tools 3 | ---------------------------------------------------------------------- 4 | 5 | local os = require 'os' 6 | local io = require 'io' 7 | local paths = require 'paths' 8 | 9 | sys = {} 10 | 11 | -------------------------------------------------------------------------------- 12 | -- load all functions from lib 13 | -------------------------------------------------------------------------------- 14 | local _lib = require 'libsys' 15 | for k,v in pairs(_lib) do 16 | sys[k] = v 17 | end 18 | 19 | -------------------------------------------------------------------------------- 20 | -- tic/toc (matlab-like) timers 21 | -------------------------------------------------------------------------------- 22 | local __t__ 23 | function sys.tic() 24 | __t__ = sys.clock() 25 | end 26 | function sys.toc(verbose) 27 | local __dt__ = sys.clock() - __t__ 28 | if verbose then print(__dt__) end 29 | return __dt__ 30 | end 31 | 32 | -------------------------------------------------------------------------------- 33 | -- execute an OS command, but retrieves the result in a string 34 | -------------------------------------------------------------------------------- 35 | local function execute(cmd) 36 | local cmd = cmd .. ' 2>&1' 37 | local f = io.popen(cmd) 38 | local s = f:read('*all') 39 | f:close() 40 | s = s:gsub('^%s*',''):gsub('%s*$','') 41 | return s 42 | end 43 | sys.execute = execute 44 | 45 | -------------------------------------------------------------------------------- 46 | -- execute an OS command, but retrieves the result in a string 47 | -- side effect: file in /tmp 48 | -- this call is typically more robust than the one above (on some systems) 49 | -------------------------------------------------------------------------------- 50 | function sys.fexecute(cmd, readwhat) 51 | readwhat = readwhat or '*all' 52 | local tmpfile = os.tmpname() 53 | local cmd = cmd .. ' >'.. tmpfile..' 2>&1' 54 | os.execute(cmd) 55 | local file = _G.assert(io.open(tmpfile)) 56 | local s= file:read(readwhat) 57 | file:close() 58 | s = s:gsub('^%s*',''):gsub('%s*$','') 59 | os.remove(tmpfile) 60 | return s 61 | end 62 | 63 | -------------------------------------------------------------------------------- 64 | -- returns the name of the OS in use 65 | -- warning, this method is extremely dumb, and should be replaced by something 66 | -- more reliable 67 | -------------------------------------------------------------------------------- 68 | function sys.uname() 69 | if paths.dirp('C:\\') then 70 | return 'windows' 71 | else 72 | local ffi = require 'ffi' 73 | local os = ffi.os 74 | if os:find('Linux') then 75 | return 'linux' 76 | elseif os:find('OSX') then 77 | return 'macos' 78 | else 79 | return '?' 80 | end 81 | end 82 | end 83 | sys.OS = sys.uname() 84 | 85 | -------------------------------------------------------------------------------- 86 | -- ls (list dir) 87 | -------------------------------------------------------------------------------- 88 | sys.ls = function(d) d = d or ' ' return execute('ls ' ..d) end 89 | sys.ll = function(d) d = d or ' ' return execute('ls -l ' ..d) end 90 | sys.la = function(d) d = d or ' ' return execute('ls -a ' ..d) end 91 | sys.lla = function(d) d = d or ' ' return execute('ls -la '..d) end 92 | 93 | -------------------------------------------------------------------------------- 94 | -- prefix 95 | -------------------------------------------------------------------------------- 96 | local function find_prefix() 97 | if arg then 98 | for i, v in pairs(arg) do 99 | if type(i) == "number" and type(v) == "string" and i <= 0 then 100 | local lua_path = paths.basename(v) 101 | if lua_path == "luajit" or lua_path == "lua" then 102 | local bin_dir = paths.dirname(v) 103 | if paths.basename(bin_dir) == "bin" then 104 | return paths.dirname(bin_dir) 105 | else 106 | return bin_dir 107 | end 108 | end 109 | end 110 | end 111 | end 112 | return "" 113 | end 114 | sys.prefix = find_prefix() 115 | 116 | -------------------------------------------------------------------------------- 117 | -- always returns the path of the file running 118 | -------------------------------------------------------------------------------- 119 | sys.fpath = require 'sys.fpath' 120 | 121 | -------------------------------------------------------------------------------- 122 | -- split string based on pattern pat 123 | -------------------------------------------------------------------------------- 124 | function sys.split(str, pat) 125 | local t = {} -- NOTE: use {n = 0} in Lua-5.0 126 | local fpat = "(.-)" .. pat 127 | local last_end = 1 128 | local s, e, cap = str:find(fpat, last_end) 129 | while s do 130 | if s ~= 1 or cap ~= "" then 131 | _G.table.insert(t,cap) 132 | end 133 | last_end = e+1 134 | s, e, cap = str:find(fpat, last_end) 135 | end 136 | if last_end <= #str then 137 | cap = str:sub(last_end) 138 | _G.table.insert(t, cap) 139 | end 140 | return t 141 | end 142 | 143 | -------------------------------------------------------------------------------- 144 | -- check if a number is NaN 145 | -------------------------------------------------------------------------------- 146 | function sys.isNaN(number) 147 | -- We rely on the property that NaN is the only value that doesn't equal itself. 148 | return (number ~= number) 149 | end 150 | 151 | -------------------------------------------------------------------------------- 152 | -- sleep 153 | -------------------------------------------------------------------------------- 154 | function sys.sleep(seconds) 155 | sys.usleep(seconds*1000000) 156 | end 157 | 158 | -------------------------------------------------------------------------------- 159 | -- colors, can be used to print things in color 160 | -------------------------------------------------------------------------------- 161 | sys.COLORS = require 'sys.colors' 162 | 163 | -------------------------------------------------------------------------------- 164 | -- backward compat 165 | -------------------------------------------------------------------------------- 166 | sys.dirname = paths.dirname 167 | sys.concat = paths.concat 168 | 169 | return sys 170 | -------------------------------------------------------------------------------- /sys-1.0-0.rockspec: -------------------------------------------------------------------------------- 1 | package = "sys" 2 | version = "1.0-0" 3 | 4 | source = { 5 | url = "git://github.com/torch/sys", 6 | tag = "1.0-0" 7 | } 8 | 9 | description = { 10 | summary = "A system library for Torch", 11 | detailed = [[ 12 | Provides system functionalities for Torch. 13 | ]], 14 | homepage = "https://github.com/torch/sys", 15 | license = "BSD" 16 | } 17 | 18 | dependencies = { 19 | "torch >= 7.0", 20 | } 21 | 22 | build = { 23 | type = "command", 24 | build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], 25 | install_command = "cd build && $(MAKE) install" 26 | } 27 | -------------------------------------------------------------------------------- /sys-1.1-0.rockspec: -------------------------------------------------------------------------------- 1 | package = "sys" 2 | version = "1.1-0" 3 | 4 | source = { 5 | url = "git://github.com/torch/sys" 6 | } 7 | 8 | description = { 9 | summary = "A system library for Torch", 10 | detailed = [[ 11 | Provides system functionalities for Torch. 12 | ]], 13 | homepage = "https://github.com/torch/sys", 14 | license = "BSD" 15 | } 16 | 17 | dependencies = { 18 | "torch >= 7.0", 19 | "luaffi" 20 | } 21 | 22 | build = { 23 | type = "command", 24 | build_command = [[cmake -E make_directory build && cd build && cmake .. -DLUALIB=$(LUALIB) -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], 25 | install_command = "cd build && $(MAKE) install" 26 | } 27 | -------------------------------------------------------------------------------- /sys.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #ifdef _WIN32 5 | 6 | #define WINDOWS_LEAN_AND_MEAN 7 | #include 8 | #include 9 | 10 | static int l_clock(lua_State *L) { 11 | static const uint64_t EPOCH = 116444736000000000ULL; 12 | SYSTEMTIME systemtime; 13 | FILETIME filetime; 14 | uint64_t time; 15 | GetSystemTime(&systemtime); 16 | GetSystemTimeAsFileTime(&filetime); 17 | time = (((uint64_t)filetime.dwHighDateTime) << 32) + ((uint64_t)filetime.dwLowDateTime); 18 | double precise_time = (time - EPOCH) / 10000000.0; 19 | lua_pushnumber(L, precise_time); 20 | return 1; 21 | } 22 | 23 | static int l_usleep(lua_State *L) { 24 | int time = 1; 25 | if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1); 26 | Sleep(time / 1000); 27 | return 1; 28 | } 29 | 30 | #else 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | static int l_clock(lua_State *L) { 42 | struct timeval tv; 43 | struct timezone tz; 44 | struct tm *tm; 45 | gettimeofday(&tv, &tz); 46 | tm=localtime(&tv.tv_sec); 47 | double precise_time = tv.tv_sec + tv.tv_usec / 1e6; 48 | lua_pushnumber(L,precise_time); 49 | return 1; 50 | } 51 | 52 | static int l_usleep(lua_State *L) { 53 | int time = 1; 54 | if (lua_isnumber(L, 1)) time = lua_tonumber(L, 1); 55 | usleep(time); 56 | return 1; 57 | } 58 | 59 | #endif 60 | 61 | static const struct luaL_Reg routines [] = { 62 | {"clock", l_clock}, 63 | {"usleep", l_usleep}, 64 | {NULL, NULL} 65 | }; 66 | 67 | #if defined(_WIN32) 68 | #define SYS_DLLEXPORT __declspec(dllexport) __cdecl 69 | #else 70 | #define SYS_DLLEXPORT 71 | #endif 72 | int SYS_DLLEXPORT luaopen_libsys(lua_State *L) 73 | { 74 | lua_newtable(L); 75 | #if LUA_VERSION_NUM == 501 76 | luaL_register(L, NULL, routines); 77 | #else 78 | luaL_setfuncs(L, routines, 0); 79 | #endif 80 | return 1; 81 | } 82 | --------------------------------------------------------------------------------